﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using RM2012Server.Business;
using System.Data.SqlClient;

namespace RM2012Server.Model
{
    public class PermissionDAO
    {
        DataServices m_PermissionDAO = new DataServices();

        public DataTable GetAll()
        {
            m_PermissionDAO.Load("GetAllPermission", null);
            return m_PermissionDAO;
        }

        public int Add(PermissionInfo info)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param[0].Value = info.UserId;

            param[1] = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param[1].Value = info.UserGroupId;

            return m_PermissionDAO.ExecuteUpdate("AddPermission", param);
        }

        public int Update(PermissionInfo info)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param[0].Value = info.UserId;

            param[1] = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param[1].Value = info.UserGroupId;

            return m_PermissionDAO.ExecuteUpdate("UpdatePermission", param);
        }

        public int DeleteByUser(String userId)
        {
            SqlParameter param = new SqlParameter("@UserId", SqlDbType.VarChar, 6);
            param.Value = userId;

            return m_PermissionDAO.ExecuteUpdate("DeletePermissionByUserId", param);
        }

        public int DeleteByGroup(String userGroupId)
        {
            SqlParameter param = new SqlParameter("@UserGroupId", SqlDbType.VarChar, 4);
            param.Value = userGroupId;

            return m_PermissionDAO.ExecuteUpdate("DeletePermissionByUserGroupId", param);
        }

        public DataRow AddNewRow()
        {
            return m_PermissionDAO.NewRow();
        }

        public void AddNewPermission(DataRow m_Row)
        {
            m_PermissionDAO.Rows.Add(m_Row);
        }

        public bool SaveData()
        {
            return m_PermissionDAO.ExecuteNoneQuery() > 0;
        }
    }
}
